<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include("base.php");
  
  class ByeController extends BaseController
  {  
    var $_name = 'bye';
    
    function add(){
    
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel('team','teammodel'));
      
      parent::add();
    }
    
    function edit(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel('byeteam','byeteammodel'));
      $view->setModel($this->getModel('team','teammodel'));
      
      parent::edit();
    }
    
    function remove()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array');
      if($ids === null)
        JError::raiseError(500, 'cid parameter missing from the request');
       
      $bye_team = & $this->getModel('byeteam', 'byeteammodel');
      foreach($ids as $id){
        $bye_teams = $bye_team->get_by($id);
        $bt_ids           = array();
        foreach($bye_teams as $bt)
          array_push($bt_ids, $bt->id);
        if(!empty($bt_ids))
          $bye_team->delete($bt_ids);
      }
      parent::remove();
    }
    
    function save()
    {
      $data = JRequest::get('POST');
      
      $model  = & $this->getModel($this->_name); 
      $bye = $model->save($data);
      
      $bye_team = $this->getModel('byeteam', 'byeteammodel');
      $bye_team->renew_teams($bye->id, $data['teams']);
      
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');             
    }
  }
?>